package buildings.net.server.sequental;

import buildings.net.server.exceptions.BuildingUnderArrestException;
import buildings.*;
import buildings.dwelling.Dwelling;
import buildings.dwelling.DwellingFactory;
import buildings.dwelling.hotel.Hotel;
import buildings.dwelling.hotel.HotelFactory;
import buildings.office.OfficeBuilding;
import buildings.office.OfficeFactory;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class BinaryServer {

    public static void main(String[] args) {
        Socket clientSocket = null;
        while (true) {
            try {
                ServerSocket server = new ServerSocket(6667, 0, InetAddress.getByName("localhost"));
                clientSocket = server.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(
                        clientSocket.getInputStream()));
                PrintWriter out =
                        new PrintWriter(clientSocket.getOutputStream(), true);
                Building buf = null;
                float cost = 0;
                String type = null;
                String str = null;
                while (true) {
                    type = in.readLine();
                    switch (type) {
                        case "dwelling":
                            Buildings.setBuildingFactory(new DwellingFactory());
                            buf = Buildings.readBuilding(in);
                            try {
                                str = Float.toString(getCost((Dwelling) buf));
                            } catch (BuildingUnderArrestException e) {
                                str = "Здание под арестом!";
                            }
                            break;
                        case "office":
                            Buildings.setBuildingFactory(new OfficeFactory());
                            buf = Buildings.readBuilding(in);
                            try {
                                str = Float.toString(getCost((Dwelling) buf));
                            } catch (BuildingUnderArrestException e) {
                                str = "Здание под арестом!";
                            }
                            break;
                        case "hotel":
                            Buildings.setBuildingFactory(new HotelFactory());
                            buf = Buildings.readBuilding(in);
                            try {
                                str = Float.toString(getCost((Dwelling) buf));
                            } catch (BuildingUnderArrestException e) {
                                str = "Здание под арестом!";
                            }
                            break;
                    }
                    if (!type.equals("end")) {
                        out.println(str);
                    } else {
                        break;
                    }
                }

            } catch (IOException e) {
            }
        }
    }

    private static float getCost(Dwelling b)
            throws BuildingUnderArrestException {
        if (!isArested()) {
            return b.getTotalSquare() * 1000;
        }
        throw new BuildingUnderArrestException();
    }

    private static float getCost(OfficeBuilding b)
            throws BuildingUnderArrestException {
        return b.getTotalSquare() * 1500;
    }

    private static float getCost(Hotel b)
            throws BuildingUnderArrestException {
        return b.getTotalSquare() * 2000;
    }

    private static boolean isArested() {
        Random r = new Random();
        int buf = r.nextInt(100);
        if (buf < 10) {
            return true;
        }
        return false;
    }
}
